<template>
  <div>
    <el-form ref="form" :model="model" :rules="validatorRules">
      <el-form-item required prop="mobile">
        <el-input v-model="model.mobile" size="large" type="text" placeholder="请输入手机号">
          <el-icon type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }"/>
        </el-input>
      </el-form-item>
      <el-row :gutter="16">
        <el-col class="gutter-row" :span="16">
          <el-form-item required prop="captcha">
            <el-input v-model="model.captcha" size="large" type="text" placeholder="请输入验证码">
              <el-icon type="mail" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col class="gutter-row" :span="8">
          <el-button
              class="getCaptcha"
              tabindex="-1"
              :disabled="state.smsSendBtn"
              @click.stop.prevent="getCaptcha"
              :text="!state.smsSendBtn && '获取验证码' || (state.time+' s')"></el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script lang="ts">
export default {
  name: 'LoginPhone',
  data() {
    return {
      model: {
        mobile: '',
        captcha: ''
      },
      //手机号登录用
      state: {
        time: 60,
        smsSendBtn: false,
      },
      validatorRules: {
        mobile: [
          {required: true, message: '请输入手机号码!'},
          {validator: this.validateMobile}
        ],
        captcha: [{
          required: true, message: '请输入验证码!'
        }]
      }

    }
  },
  methods: {
    handleLogin(rememberMe: any) {
      this.validateFields(['mobile', 'captcha'], (err: any) => {
        if (!err) {
          let loginParams = {
            mobile: this.model.mobile,
            captcha: this.model.captcha,
            remember_me: rememberMe
          }

        } else {
          this.$emit('validateFail')
        }
      })
    },
    // 校验手机号
    validateMobile(rule: string, value: string, callback: any) {
      if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
        callback();
      } else {
        callback("您的手机号码格式不正确!");
      }
    },
    // 获取验证码
    getCaptcha(e: any) {
      e.preventDefault();
      let that = this;
      that.validateFields(['mobile'], (err: any) => {
            if (!err) {
              that.state.smsSendBtn = true;
              let interval = window.setInterval(() => {
                if (that.state.time-- <= 0) {
                  that.state.time = 60;
                  that.state.smsSendBtn = false;
                  window.clearInterval(interval);
                }
              }, 1000);

              const hide = that.$message.loading('验证码发送中..', 0);
              let smsParams: { mobile: string, smsMode: string };
              smsParams.mobile = that.model.mobile;
              smsParams.smsMode = "0";
            }
          }
      );
    },
    cmsFailed(err: any) {
      this.$notification['error']({
        message: '获取验证码失败',
        description: err,
        duration: 4,
      });
    },
    /**
     * 验证字段
     * @param arr
     * @param callback
     */
    validateFields(arr: any, callback: any) {
      let promiseArray = []
      for (let item of arr) {
        let p = new Promise((resolve, reject) => {
          this.$refs['form'].validateField(item, (err: any) => {
            if (!err) {
              resolve('');
            } else {
              reject(err);
            }
          })
        });
        promiseArray.push(p)
      }
      Promise.all(promiseArray).then(() => {
        callback()
      }).catch(err => {
        callback(err)
      })
    },
  }
}
</script>

<style scoped>
.getCaptcha {
  display: block;
  width: 100%;
  height: 40px;
}
</style>
